#include "stdint.h"
#include "stdbool.h"
#include "config.h" // contains all fft related

void benchmark(void) {
  unsigned n;
  fft_complex_t complex[WAVE_SIZE >> 1];
  unsigned fft_size = WAVE_SIZE;
  unsigned fft_bits = WAVE_BITS;
	unsigned size;
  samp16bit *p_table;
  samp32bit *p_tcplx;
	
  // This loop generates a mip-mapped wavetable
	// First iteration all frequencies
	// Second iteration half frequencies, half size
	// Third quarter/quarter, etc.
	while(fft_size > 2) {
    fft_size >>= 1;
    fft_bits--;

    // Construct sawtooth
		// This could be any combination of harmonics, sawtooth is just example
		// fft_sym_magnitude makes complex numbers from 2 real data points and does bit reversal
    complex[0].r = complex[0].i = 0;
    for(n = 1; n <= fft_size >> 1; n++) {
      unsigned z = fft_size - n; // n:z is the symmetrical pair for real data
      fft_sym_magnitude(complex, fft_bits, n, 0x10000000 / n, 0x10000000 / z, true);
    }

		// Perform IFFT (data is already bit-reversed)
    fft_inverse(complex, fft_size);
    // Prepare for copy to wavetable
    size = fft_size << 1;
    p_table = &wavetable[size];
    p_tcplx = (samp32bit*)complex;
    // Copy and apply wavefolding
    while(size--) *p_table++ = (*p_tcplx++) >> 16;
  }
}
